//
//  OMCategoryFilterView.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/18.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMCategoryFilterView.h"

#import "OMCategoryFilterSelectionView.h"

@interface OMArrowElementView:BaseView
@property (nonatomic, strong) UILabel *elementLabel;
@property (nonatomic, strong) UIButton *arrowButton;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, copy) void (^tapHandler)(OMArrowElementView *arrowView);
@property (nonatomic, assign) BOOL selected;
@end

@implementation OMArrowElementView


- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    self.elementLabel.text = title;
}

- (void)configure
{
    @weakify(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:nil action:nil];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        @strongify(self);
        if (self.tapHandler) {
            self.tapHandler(self);
        }
    }];
    self.userInteractionEnabled = true;
    [self addGestureRecognizer:tap];

    UIView *containView = [UIView new];
    [self addSubview:containView];
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.centerX.equalTo(self.mas_centerX);
    }];

    _elementLabel = [UILabel new];
    _elementLabel.font = SM_CFont(30);
    _elementLabel.textColor = HEXCOLOR(@"#232323");
    _elementLabel.text = @"综合排序";
    [containView addSubview:self.elementLabel];
    [_elementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@0);
    }];

    _arrowButton = [UIButton new];
    _arrowButton.userInteractionEnabled = false;
    [_arrowButton setImage:[[UIImage imageNamed:@"order_list_arrow_15x15_"] imageWithColor:HEXCOLOR(@"#CECECE")] forState:UIControlStateNormal];
    [containView addSubview:self.arrowButton];
    [_arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_elementLabel.mas_right).offset(SM_SIZE(10));
        make.right.equalTo(@0);
        make.centerY.equalTo(containView.mas_centerY);
    }];
}

- (void)setSelected:(BOOL)selected
{
    _selected = selected;
    self.arrowButton.transform = selected? CGAffineTransformMakeRotation(M_PI): CGAffineTransformIdentity;
}


@end

@interface OMCategoryFilterView () <OMCategoryFilterSelectionViewDelegate>
@property (nonatomic, strong) NSMutableArray<OMArrowElementView *> *filters;
@property (nonatomic, strong) UIButton *layoutButton;
@property (nonatomic, assign) NSInteger raisedIndex;
@property (nonatomic, weak) OMCategoryFilterSelectionView *selectView;
@end


@implementation OMCategoryFilterView

- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}

- (void)setItems:(NSArray *)items
{
    _items = items;
    _raisedIndex = -1;
    [self resignSelectView];
    for (UIView *filter in self.filters) {
        [filter removeFromSuperview];
    }
    [_filters removeAllObjects];

    NSUInteger i = 0;
    for (NSDictionary *dic in items) {
        OMArrowElementView *arrowView = [OMArrowElementView new];
        arrowView.title = dic[@"title"];
        [_filters addObject:arrowView];
        [self addSubview:arrowView];
        @weakify(self);
        [arrowView setTapHandler:^(OMArrowElementView *tempArrowV) {
            @strongify(self);
            [self didTapArrowViewAtIndex:i];
        }];
        [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.equalTo(@(SM_SIZE(10)));
            }else {
                make.left.equalTo(self.filters[i-1].mas_right).offset(SM_SIZE(0));
                make.width.equalTo(self.filters[i-1].mas_width);
            }
            if (i == items.count - 1) {
                make.right.equalTo(self.layoutButton.mas_left).offset(SM_SIZE(-20));
            }
            make.top.bottom.equalTo(@0);
        }];
        i ++;
    }
}


- (void)configure
{

    _filters = [NSMutableArray arrayWithCapacity:0];

    _layoutButton = [UIButton new];
    [_layoutButton setImage:[UIImage imageNamed:@"selectCategroy"] forState:UIControlStateNormal];
    [_layoutButton setImage:[UIImage imageNamed:@"selectCategroyOneLine"] forState:UIControlStateSelected];
    [self addSubview:_layoutButton];
    CGFloat edge = SM_SIZE(20);
    _layoutButton.contentEdgeInsets = UIEdgeInsetsMake(edge, edge, edge, edge);
    @weakify(self);
    [[_layoutButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *button) {
        @strongify(self);
        button.selected = !button.selected;
        if (self.layoutHandler) {
            self.layoutHandler();
        }
        [self resignSelectView];
    }];
    [_layoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(0)));
        make.centerY.equalTo(self.mas_centerY);
    }];

    UIView *verticalLine = [UIView new];
    verticalLine.backgroundColor = HEXCOLOR(@"#DBDBDB");
    [self addSubview:verticalLine];
    [verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_layoutButton.mas_left).offset(SM_SIZE(0));
        make.width.equalTo(@.5f);
        make.top.equalTo(@(SM_SIZE(25)));
        make.bottom.equalTo(@(SM_SIZE(-25)));
    }];

    UIView *line = [UIView new];
    line.backgroundColor = SM_naviShadowColor;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@(SM_SIZE(0)));
        make.height.equalTo(@(.5f));
    }];

}


/**
 下拉
 */
- (void)didTapArrowViewAtIndex:(NSUInteger)index
{
    if (_raisedIndex >= 0) {
        NSInteger tempIndex = _raisedIndex;
        [_selectView dismiss];
        if (index != tempIndex) {
            [self showSelectViewAtIndex:index];
        }
    }else {
        [self showSelectViewAtIndex:index];
    }
}

- (void)showSelectViewAtIndex:(NSUInteger)index
{
    _raisedIndex = index;
    self.filters[_raisedIndex].selected = true;
    OMCategoryFilterSelectionView *selectView = [OMCategoryFilterSelectionView new];
    _selectView = selectView;
    selectView.itemTitles = self.items[index][@"items"];
    selectView.delegate = self;
    selectView.selectedTitle = self.filters[_raisedIndex].title;
    [selectView showInView:self.superview
                 withFrame:CGRectMake(self.frame.origin.x+1,
                                      CGRectGetMaxY(self.frame),
                                      SM_SCREEN_WIDTH*6/8,
                                      SM_SCREEN_HEIGHT-CGRectGetMaxY(self.frame)- (Navigator.currentNavigationController.viewControllers.count == 1?49:0))];
}

- (void)resignSelectView
{
    if (self.selectView) {
        [self.selectView dismiss];
        self.selectView = nil;
    }
}

//MARK: OMCategoryFilterSelectionView delegate
- (void)didDismissFilterSelectView:(OMCategoryFilterSelectionView*)selectView
{
    if (self.filters.count > _raisedIndex) {
        self.filters[_raisedIndex].selected = false;
    }
    _raisedIndex = -1;
    _selectView = nil;
}

- (void)filterSelectView:(OMCategoryFilterSelectionView*)selectView didSelectItemAtIndex:(NSInteger)index
{
    self.filters[_raisedIndex].title = self.items[_raisedIndex][@"items"][index];
    if (self.selectionHandler) {
        self.selectionHandler(_raisedIndex,index);
    }
}
@end
